package com.sf.fy.uav.withdrawal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.withdrawal.entity.WithdrawalStatistics;
import com.sf.fy.uav.withdrawal.mapper.WithdrawalStatisticsMapper;
import com.sf.fy.uav.withdrawal.service.IWithdrawalStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 提现记录表 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-08-31
 */
@Service
public class WithdrawalStatisticsServiceImpl extends ServiceImpl<WithdrawalStatisticsMapper, WithdrawalStatistics> implements IWithdrawalStatisticsService {
    @Autowired
    private WithdrawalStatisticsMapper withdrawalStatisticsMapper;

    @Override
    public WithdrawalStatistics getStatistics(String userId, Date statisticsDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<WithdrawalStatistics> withdrawalStatisticsQueryWrapper = new QueryWrapper<>();
        //需要建组合唯一索引
        withdrawalStatisticsQueryWrapper.eq("user_id", userId);
        try {
            withdrawalStatisticsQueryWrapper.eq("statistics_date", sdf.parse(sdf.format(statisticsDate)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        WithdrawalStatistics withdrawalStatistics = withdrawalStatisticsMapper.selectOne(withdrawalStatisticsQueryWrapper);
        if (ObjectUtils.isEmpty(withdrawalStatistics)) {
            Date date = new Date();
            withdrawalStatistics = new WithdrawalStatistics();
            withdrawalStatistics.setCreateTime(date);
            withdrawalStatistics.setUpdateTime(date);
            withdrawalStatistics.setStatisticsDate(date);
            withdrawalStatistics.setUserId(userId);
            withdrawalStatistics.setWithdrawalAmount(new BigDecimal(0));
            withdrawalStatisticsMapper.insert(withdrawalStatistics);
        }
        return withdrawalStatistics;
    }
}
